package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.RepoGoodsEntity;
import cn.mumzone.pos.model.RepoGoodsSpecEntity;
import cn.mumzone.pos.model.RepoListGoodsEntity;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;

/**
 * Created by qiuzhenhuan on 2019/7/31.
 */

public class ADA_RepoGoods extends CommonAdapter<RepoListGoodsEntity> {

    private Context context;
    private ViewHolder mPreHolder = null;

    public ADA_RepoGoods(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_repo_goods;
    }

    @Override
    protected void convert(final ViewHolder holder, RepoListGoodsEntity repoGoodsEntity, final int position) {

        if (mPreHolder == null && position == 1) {
            holder.getView(R.id.root_view).setBackgroundResource(R.drawable.shape_bg_stock_search_selected);
            mPreHolder = holder;
        }else {
            holder.getView(R.id.root_view).setBackgroundResource(R.drawable.shape_bg_white_with_corner_6dp);
        }

        holder.setText(R.id.tv_good_name, StringUtils.get(repoGoodsEntity.getGoodsName()));
        holder.setText(R.id.tv_good_spec, getFormatGoodsSpec(repoGoodsEntity.getParameter()));

        String goodsQuantity = "0";
        String goodsUnit = "";
        if (!ListUtil.isListEmpty(repoGoodsEntity.getGoodsBatch())) {
            goodsQuantity = repoGoodsEntity.getGoodsBatch().get(0).getGoodsQuantity();
            goodsUnit = repoGoodsEntity.getGoodsBatch().get(0).getGoodsUnit();
        }
        holder.setText(R.id.tv_not_batch_good_num, String.format("%s%s", goodsQuantity, goodsUnit));
        holder.setText(R.id.tv_good_num, String.format("%s%s", goodsQuantity, goodsUnit));
        holder.setText(R.id.tv_batch_num, String.format("批次数：%s个", ListUtil.isListEmpty(repoGoodsEntity.getGoodsBatch())
                ? "0" : repoGoodsEntity.getGoodsBatch().size()));

        if (!ListUtil.isListEmpty(repoGoodsEntity.getGoodsBatch()) &&
                !StringUtils.isEmpty(repoGoodsEntity.getGoodsBatch().get(0).getGoodsBatchNumber())) {
            holder.setVisible(R.id.rl_not_batch_goods, false);
            holder.setVisible(R.id.rl_batch_goods, true);

        } else {
            holder.setVisible(R.id.rl_not_batch_goods, true);
            holder.setVisible(R.id.rl_batch_goods, false);

        }


        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPreHolder != null) {
                    mPreHolder.getView(R.id.root_view).setBackgroundResource(R.drawable.shape_bg_white_with_corner_6dp);
                }
                holder.getView(R.id.root_view).setBackgroundResource(R.drawable.shape_bg_stock_search_selected);
                mPreHolder = holder;
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    private String getFormatGoodsSpec(List<RepoGoodsSpecEntity> goodsSpec) {

        StringBuilder builder = new StringBuilder();
        if (!ListUtil.isListEmpty(goodsSpec)) {
            for (int index = 0; index < goodsSpec.size(); index++) {
                RepoGoodsSpecEntity specBean = goodsSpec.get(index);
                builder.append(specBean.getParamValue());
                if (index < goodsSpec.size() - 1) {
                    builder.append("，");
                }
            }
        }
        return StringSpanUtils.init().addForegroundColorHighLight(mContext.getResources().getColor(R.color.weak_text_color), "规格：")
                .addStr(StringUtils.get(builder.toString(), "无")).build().toString();
    }

    public void clearSelect(){
        if(mPreHolder!=null){
            mPreHolder.getView(R.id.root_view).setBackgroundResource(R.drawable.shape_bg_white_with_corner_6dp);
        }
        mPreHolder = null;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
